package com.yisu.expressway.onedollar.fragment;

import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.view.View;

import java.util.HashMap;
import java.util.List;
import java.util.ListIterator;
import java.util.Map;

import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.google.gson.reflect.TypeToken;
import com.yisu.expressway.R;
import com.yisu.expressway.network.ApiClientHelper;
import com.yisu.expressway.network.ApiResult;
import com.yisu.expressway.network.ApiSetting;
import com.yisu.expressway.onedollar.activity.OneDollarMainActivity;
import com.yisu.expressway.onedollar.activity.OrderShowBuildActivity;
import com.yisu.expressway.onedollar.activity.UserAddressActivity;
import com.yisu.expressway.onedollar.activity.UserAddressBuildActivity;
import com.yisu.expressway.onedollar.model.MyOrderItem;
import com.yisu.expressway.onedollar.model.UserAddressItem;

import org.json.JSONObject;

import static android.app.Activity.RESULT_OK;

/**
 * Created by Kaven on 2016/12/19.
 */

public class WinningOrderFragment extends BaseMyOrderFragment {

    @Override
    protected void onActionClick(View view) {
        if (TAG_ACTION_NET_ERR.equals(view.getTag())){
            mRecyclerView.refreshData();
        }else if (TAG_ACTION_NORMAL.equals(view.getTag())){
            startTargetActivity(OneDollarMainActivity.class);
        }
    }

    @Override
    protected Drawable getNetErrorDrawable() {
        return getResources().getDrawable(R.drawable.ic_no_signal);
    }

    @Override
    protected String getNetErrorDes() {
        return getString(R.string.one_dollar_empty_net_error);
    }

    @Override
    protected String getNetErrorAction() {
        return getString(R.string.one_dollar_empty_action_retry);
    }

    @Override
    protected List<MyOrderItem> filterData(List<MyOrderItem> source) {
        if (source == null || source.isEmpty()) {
            return source;
        }
        ListIterator<MyOrderItem> listIterator = source.listIterator();
        while (listIterator.hasNext()) {
            MyOrderItem next = listIterator.next();
            if (next.status != MyOrderItem.STATUS_WINNED) {
                listIterator.remove();
            }
        }
        return source;
    }

    @Override
    protected void fetchDataFromServer(int start, int pageSize) {
        if (pageSize <= 0) {
            return;
        }
        Map<String, Object> params = new HashMap();

        int pageIndex = getPageIndex(start, pageSize);

        params.put("pageindex", pageIndex);
        params.put("pagesize", pageSize);
        params.put("grabStatus", 2);
        ApiClientHelper.postApi(ApiSetting.getMyOrderList(), new TypeToken<List<MyOrderItem>>() {
        }, new JSONObject(params), new Response.Listener<ApiResult<List<MyOrderItem>>>() {
            @Override
            public void onResponse(ApiResult<List<MyOrderItem>> result) {
                if (!result.success) {
                    mRecyclerView.dataFetchFail(result.getMsg());
                    showEmptyView(true, true);
                    return;
                }
                List<MyOrderItem> datas = filterData(result.getResult());
                if (datas == null || datas.isEmpty()) {
                    showEmptyView(true, false);
                } else {
                    mRecyclerView.dataFetchDone(datas);
                }

                if (mAnimation != null) {
                    mRefreshLayout.setRefreshing(false);
                    mAnimation.stop();
                    carImage.setImageResource(R.color.colorTransparent);
                }
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {
                mRecyclerView.dataFetchFail(volleyError.getMessage());
                showEmptyView(true, true);
            }
        }, this);
    }

    @Override
    protected Drawable getEmptyDrawable() {
        return getResources().getDrawable(R.drawable.ic_no_winning_record);
    }

    @Override
    protected String getEmptyAction() {
        return getString(R.string.one_dollar_empty_action);
    }

    @Override
    protected String getEmptyDes() {
        return getString(R.string.one_dollar_empty_winning_record);
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (RESULT_OK == resultCode) {
            switch (requestCode) {
                case UserAddressBuildActivity.BUILD_TYPE_INSERT:
                    UserAddressItem userAddressItem = (UserAddressItem) data
                        .getSerializableExtra(UserAddressBuildActivity.EXTRA_KEY_RESULT);

                    long orderId = data
                        .getLongExtra(UserAddressBuildActivity.EXTRA_KEY_RESULT_ORDER_ID, -1);
                    changeOrderAddress(userAddressItem, orderId);
                    break;
                case UserAddressActivity.REQUEST_CODE_SELECT:
                    userAddressItem = (UserAddressItem) data
                        .getSerializableExtra(UserAddressActivity.EXTRA_KEY_RESULT);

                    orderId = data
                        .getLongExtra(UserAddressActivity.EXTRA_KEY_RESULT_ORDER_ID, -1);
                    changeOrderAddress(userAddressItem, orderId);
                    break;
                case OrderShowBuildActivity.REQUEST_CODE_NORMAL:
                    mRecyclerView.refreshData();
                    break;
                default:
                    break;
            }
        }
    }
}
